﻿//======================================================
//==     (c)2008 aspxcms inc by NeTCMS v1.0              ==
//==          Forum:bbs.aspxcms.com                   ==
//==         Website:www.aspxcms.com                  ==
//======================================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using NetCMS.Content;

public partial class manage_channel_style_list : NetCMS.Web.UI.ManagePage
{
    public manage_channel_style_list()
    {
        this.Authority_Code = "D011";
    }
    Channel rd = new Channel();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.CheckAdminAuthority();
        Response.CacheControl = "no-cache";
        this.PageNavigator1.OnPageChange += new PageChangeHandler(PageNavigator1_PageChange);
        if (!IsPostBack)
        {
            if (SiteID != "0")
            {
                PageError("分站没权限操作", "javascript:history.back();", true);
            }
            string ChID = Request.QueryString["ChID"];
            string alock = Request.QueryString["alock"];
            if (alock != string.Empty && alock != null)
            {
                string id = Request.QueryString["id"];
                switch (alock.ToString())
                {
                    case "0":
                        rd.lockStyleContent(int.Parse(id.ToString()), int.Parse(ChID.ToString()), 0);
                        PageRight("解锁成功！", "javascript:history.back();", true);
                        break;
                    case "1":
                        rd.lockStyleContent(int.Parse(id.ToString()), int.Parse(ChID.ToString()), 1);
                        PageRight("锁定成功！", "javascript:history.back();", true);
                        break;
                }
            }
            int sClassID = 0;
            string iclassID = "#0";
            string ClassID = Request.QueryString["ClassID"];
            if (ClassID != string.Empty && ClassID != null)
            {
                sClassID = int.Parse(ClassID.ToString());
                iclassID = ClassID.ToString();
            }
            else
            {
                classAll.Visible = false;
            }
            copyright.InnerHtml = CopyRight;
            GetstyleClassList((this.gClassID), 0, 0, sClassID);
            StartLoad(1, "#0", iclassID);
        }
    }
    protected void PageNavigator1_PageChange(object sender, int PageIndex)
    {
        string iclassID = "#0";
        if (ClassID != string.Empty && ClassID != null)
        {
            iclassID = ClassID.ToString();
        }
        StartLoad(PageIndex, "#0", iclassID);
    }

    //数据初始化
    protected void StartLoad(int PageIndex, string keywords,string ClassID)
    {
        int i, j;
        string ChID = Request.QueryString["ChID"];
        string gkeywords = "#0";
        if (keywords != "#0")
        {
            gkeywords = keywords;
        }
        string gClassID = "#0";
        if (ClassID != "#0")
        {
            gClassID = ClassID;
        }
        DataTable dt = rd.GetStylePage(gkeywords,gClassID, int.Parse(Request.QueryString["ChID"]), PageIndex, 50, out i, out j, null);
        this.PageNavigator1.PageCount = j;
        this.PageNavigator1.PageIndex = PageIndex;
        this.PageNavigator1.RecordCount = i;
        string strchar = string.Empty;
        string PoPstr = string.Empty;
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Columns.Add("GstyleName", typeof(string));
            dt.Columns.Add("islocks", typeof(string));
            dt.Columns.Add("ClassName", typeof(string));
            dt.Columns.Add("contents", typeof(string));
            dt.Columns.Add("op", typeof(string));
            for (int k = 0; k < dt.Rows.Count; k++)
            {
                string tmContent = dt.Rows[k]["styleContent"].ToString();
                tmContent = tmContent.ToLower().Replace("<", "&lt;");
                tmContent = tmContent.ToLower().Replace(">", "&gt;");
                tmContent = Regex.Replace(tmContent, @"<img(.+?){(.+?)}(.+?)>", "<img src=\"../../SysImages/folder/spic.png\" border=\"0\" title=\"样式中的标签\" />", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                dt.Rows[k]["contents"] = tmContent;
                string sdesc = string.Empty;
                if (dt.Rows[k]["styleDescript"].ToString().Trim() != string.Empty)
                {
                    sdesc = "<div style=\"color:#999999;font-size:11.5px;\">" + dt.Rows[k]["styleDescript"].ToString() + "</div>";
                }
                dt.Rows[k]["GstyleName"] = "<a class=\"list_link\" href=\"style_add.aspx?ChID=" + ChID.ToString() + "&id=" + dt.Rows[k]["id"].ToString() + "&Action=edit\" title=\"点击修改\">" + dt.Rows[k]["styleName"].ToString() + "</a>"+sdesc+"";
                dt.Rows[k]["ClassName"] = "<a href=\"style_list.aspx?ChID=" + ChID.ToString() + "&ClassID=" + dt.Rows[k]["ClassID"].ToString() + "\" class=\"list_link\">" + rd.getStyleClassName(int.Parse(dt.Rows[k]["classID"].ToString())) + "</a>";
                dt.Rows[k]["op"] = "<a href=\"style_add.aspx?ChID=" + ChID.ToString() + "&id=" + dt.Rows[k]["id"].ToString() + "&Action=edit\" title=\"点击修改\"><img src=\"../../sysImages/" + NetCMS.Config.UIConfig.CssPath() + "/sysico/edit.gif\" border=\"0\"></a><a href=\"javascript:preview('div_" + dt.Rows[k]["id"].ToString() + "')\"><img src=\"../../sysImages/" + NetCMS.Config.UIConfig.CssPath() + "/sysico/review.gif\" border=\"0\"></a> <input type=\"checkbox\" name=\"infoID\" value=\"" + dt.Rows[k]["id"].ToString() + "\">";
                if (dt.Rows[k]["islock"].ToString() == "1")
                {
                    dt.Rows[k]["islocks"] = "<a href=\"style_list.aspx?ChID=" + ChID.ToString() + "&id=" + dt.Rows[k]["id"].ToString() + "&alock=0\" title=\"点击解锁\" class=\"reshow\" OnClick=\"{if(confirm('确定要解锁吗？')){return true;}return false;}\">锁定</a>";
                }
                else
                {
                    dt.Rows[k]["islocks"] = "<a href=\"style_list.aspx?ChID=" + ChID.ToString() + "&id=" + dt.Rows[k]["id"].ToString() + "&alock=1\" title=\"点击锁定\" class=\"list_link\"  OnClick=\"{if(confirm('确定要锁定吗？')){return true;}return false;}\">正常</a>";
                }
               }
        }
        DataList1.DataSource = dt;
        DataList1.DataBind();
        dt.Clear(); dt.Dispose();
    }


    protected void GetstyleClassList(DropDownList lst, int ParentID, int Layer, int sClassID)
    {
        IDataReader dr = rd.getStyleClassList(ParentID, int.Parse(Request.QueryString["ChID"]));
        while (dr.Read())
        {
            ListItem it = new ListItem();
            string stxt = "";
            it.Value = dr["ID"].ToString();
            if (Layer > 0)
                stxt = "┝";
            for (int i = 1; i < Layer; i++)
            {
                stxt += " ┉ ";
            }
            it.Text = stxt + dr["cName"].ToString();
            if (sClassID == int.Parse(dr["id"].ToString()))
            {
                it.Selected = true;
            }
            lst.Items.Add(it);
            GetstyleClassList(lst, int.Parse(dr["ID"].ToString()), (Layer + 1), sClassID);
        }
        dr.Close();
    }

    protected void delContent(object sender, EventArgs e)
    {
        string Str = Request.Form["infoID"];
        if (Str == null || Str == String.Empty)
        {
            PageError("请至少选择一项!", "");
        }
        else
        {
            string[] gIDARR = Str.Split(',');
            Str = null;
            int j = 0;
            int m = 0;
            for (int i = 0; i < gIDARR.Length; i++)
            {
                if (rd.delStyleContent(int.Parse(gIDARR[i]), int.Parse(Request.QueryString["ChID"]), 0) != 0)
                {
                    j++;
                }
                else
                {
                    m++;
                }
            }
            PageRight("成功删除" + j + "个标签!,失败" + m + "个标签。", "javascript:history.back();", true);
        }
    }

    protected void Reset_allContent(object sender, EventArgs e)
    {
        int cid = 0;
        if (Request.QueryString["ClassID"] != null && Request.QueryString["ClassID"] != string.Empty)
        {
            cid = int.Parse(Request.QueryString["ClassID"]);
        }
        int DelNum = rd.delStyleContent(cid, int.Parse(Request.QueryString["ChID"]), 1);
        PageRight("成功删除" + DelNum + "个标签!", "javascript:history.back();", true);
    }

    protected void Lock_allContent(object sender, EventArgs e)
    {
        string Str = Request.Form["infoID"];
        if (Str == null || Str == String.Empty)
        {
            PageError("请至少选择一项!", "");
        }
        else
        {
            string[] gIDARR = Str.Split(',');
            Str = null;
            int j = 0;
            int m = 0;
            for (int i = 0; i < gIDARR.Length; i++)
            {
                if (rd.lockStyleContent(int.Parse(gIDARR[i]), int.Parse(Request.QueryString["ChID"]), 1) != 0)
                {
                    j++;
                }
                else
                {
                    m++;
                }
            }
            PageRight("成功锁定" + j + "个标签!,失败" + m + "个标签。", "javascript:history.back();", true);
        }
    }

    protected void UnLock_allContent(object sender, EventArgs e)
    {
        string Str = Request.Form["infoID"];
        if (Str == null || Str == String.Empty)
        {
            PageError("请至少选择一项!", "");
        }
        else
        {
            string[] gIDARR = Str.Split(',');
            Str = null;
            int j = 0;
            int m = 0;
            for (int i = 0; i < gIDARR.Length; i++)
            {
                if (rd.lockStyleContent(int.Parse(gIDARR[i]), int.Parse(Request.QueryString["ChID"]), 0) != 0)
                {
                    j++;
                }
                else
                {
                    m++;
                }
            }
            PageRight("成功解锁" + j + "个标签!,失败" + m + "个标签。", "javascript:history.back();", true);
        }
    }

    protected void Search_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string gkeywords = this.keywords.Text;
            string iclassID = "#0";
            if (Request.QueryString["ClassID"] != string.Empty && Request.QueryString["ClassID"] != null)
            {
                iclassID = Request.QueryString["ClassID"];
            }
            if (gkeywords != string.Empty & gkeywords != null)
            {
                StartLoad(1, gkeywords,  iclassID);
            }
        }
    }
}
